package com.yan.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import com.yan.controller.viewModel.login.UserModel;
import com.yan.controller.viewModel.looker.BookInfo;
import com.yan.controller.viewModel.looker.BookRatingInfo;
import com.yan.controller.viewModel.user.BorrowInfo;
import com.yan.controller.viewModel.user.ReserveInfo;
import com.yan.controller.viewModel.user.SuggestBuyInfo;
import com.yan.controller.viewModel.user.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.yan.service.BooksService;
import com.yan.vo.Page;

@Controller
@RequestMapping("/user")
public class UsersController {
	@Autowired
	private BooksService booksService;

	/**
	 * 用户主页
	 */
	@RequestMapping("/show")
	public String toShow(Model model, BookInfo book, HttpSession session) throws InstantiationException, IllegalAccessException {
		//从session对象中获取user对象
		UserModel user = (UserModel) session.getAttribute("user");
		book.setId(user.getId());
		if (book.getSid() != null) {
			//往前台传数据，可以传对象，可以传List，通过el表达式 ${}可以获取到，类似于request.setAttribute("sts",sts)效果一样
			model.addAttribute("sid", book.getSid());
		}

		if (book.getBname()!=null&&book.getBname().equals("")) {
			//将图书的名称传到jsp中
			model.addAttribute("bname", book.getBname());
			//将用户的查询信息插入查询信息表中
			booksService.insertSelectInfo(book);
		}
		//执行查询操作，将查询到的数据传到jsp
		Page page = booksService.getAllBooks(book);
		List<BookRatingInfo> list=booksService.selectBookTuijian1(user.getId());
		model.addAttribute("page", page);
		model.addAttribute("currentPage", book.getCurrentPage());
		model.addAttribute("totalPage", page.getTotalPage());
		model.addAttribute("list",list);
		
		return "/users/show";
	}

	
	/*
	 * 查看图书信息
	 */
	@RequestMapping("/borrow")
	public String toBorrow(Model model, Integer bookId) {
		//System.out.println("bookid"+bookId);
		//通过id查询图书
		BookInfo book = booksService.getBook(bookId);
		//System.out.println(book.toString());
		//将查询到的book对象传到jsp
		model.addAttribute("book", book);
		List<BookRatingInfo> list=booksService.selectBookRating(bookId);
		//System.out.println(list.get(0).toString());
		model.addAttribute("list",list);
		return "/users/borrows";
	}

	/**
	 * 执行借书操作
	 */
	@RequestMapping("/insert")
	public String insertBorrow(BorrowInfo borrow, HttpSession session) {
		//从session对象中获取user对象
		UserModel user = (UserModel) session.getAttribute("user");
		borrow.setUid(user.getId());//设置借书对象的uid
		Date date = new Date();//获取当前时间
		borrow.setStartTime(date);//设置借书对象时间
		borrow.setStatus(0);//设置借书对象状态
		booksService.insertBorrow(borrow);//执行插入操作
		
		return "redirect:/user/back";
	}

	/*
	 *查询借书表并跳到借书表界面
	 */
	@RequestMapping("/back")
	public String getBorrows(Model model, HttpSession session,BorrowInfo borrows) {
		//从session对象中获取当前user对象
		UserModel user = (UserModel) session.getAttribute("user");

		borrows.setUid(user.getId());

		//System.out.println("查询前当前页："+borrow.getCurrentPage());
		//同归user对象id查询此user所有的借书
		Page page= booksService.getBorrows(borrows);
		//System.out.println("查询后当前页："+borrow.getCurrentPage());
		model.addAttribute("page", page);//将此列表传到jsp
		
		model.addAttribute("currentPage", borrows.getCurrentPage());
		model.addAttribute("totalPage", page.getTotalPage());
		
		return "users/back";
	}

	/*
	 * 查询还书表
	 */
	@RequestMapping("/showBackBook")
	public String showBackBook(Model model, HttpSession session,BorrowInfo borrows) {
		UserModel user = (UserModel) session.getAttribute("user");
		borrows.setUid(user.getId());
		Page page= booksService.getBacks(borrows);//通过user的id查询还书表
		model.addAttribute("page", page);//将查询到的列表传到jsp中
		model.addAttribute("currentPage", borrows.getCurrentPage());
		model.addAttribute("totalPage", page.getTotalPage());
		return "users/showBackBook";
	}

	/*
	 * 图书归还
	 */
	@RequestMapping("/backBooks")
	public String backBooks(Integer id) {
		booksService.updateBacks(id); 
		return "redirect:/user/showBackBook";
	}

	/*
	 * 跳转到图书评分页面
	 */
	@RequestMapping("/pingFen")
	public String bookPingFen(Model model,Integer id) {
		
		BookInfo book=booksService.selectBackedBook(id);
		System.out.println(book);
		//将查询到的book对象传到jsp
		model.addAttribute("book", book);
		model.addAttribute("backid",id);
		return "/users/BookPingFen";
	}
	
	/*
	 * 提交评分操作
	 */
	@RequestMapping("/insertPinFen")
	public String insertPinFen(BookRatingInfo bookrating,HttpSession session,Integer backid) {
		UserModel user = (UserModel) session.getAttribute("user");
		bookrating.setUserid(user.getId());
		//System.out.println(bookrating);
		//System.out.println(backid);
		booksService.insertBookPingFen(bookrating);
		booksService.updateBackBookPingFen(backid);
		
		return "redirect:/user/showBackBook";
	}
	
	/**
	 * 
	 */
	@RequestMapping("/clickUp")
	@ResponseBody
	public String clickUp(Integer bkid,Integer uid,Model model,HttpSession session) {
		UserModel user = (UserModel) session.getAttribute("user");
		int suid=user.getId();
		String upNum=booksService.clickUp(uid, bkid, suid);
		
		return upNum;
	}
	
	@RequestMapping("/clickDown")
	@ResponseBody
	public String clickDown(Integer bkid,Integer uid,Model model,HttpSession session) {
		UserModel user = (UserModel) session.getAttribute("user");
		int suid=user.getId();
		String downNum=booksService.clickDown(uid, bkid, suid);
		return downNum;
	}
	
	/*
	* 预定书籍
	*/
	@RequestMapping("/reserve")
	public String reserveBooks(ReserveInfo reserve, HttpSession session) {
		UserModel user = (UserModel) session.getAttribute("user");
		reserve.setUid(user.getId());
		Date date = new Date();
		//System.out.println(date);
		reserve.setStartTime(date);
		
		booksService.insertReserve(reserve);
		
		return "redirect:/user/showReserveBook";
	}

	/*
	 * 查询自己预定的书籍
	 */
	@RequestMapping("/showReserveBook")
	public String showReserveBook(Model model, HttpSession session) {
		UserModel user = (UserModel) session.getAttribute("user");
		List<ReserveInfo> reserveBooks = booksService.getReserveBook(user.getId());
		model.addAttribute("reserveBooks", reserveBooks);
		return "/users/reserveBook";
	}
	
	/*
	 * 跳转到建议图书馆购买图书界面
	 */
	@RequestMapping("/suggestBuy")
	public String suggestBuy() {
		return "/users/suggestBuy";
	}
	
	
	/*
	 * 执行提交建议购买书
	 */
	@RequestMapping("/suggestBuy2")
	public String suggestBuy2(Model model, SuggestBuyInfo suggest) {

		if(!suggest.getBname().isEmpty()&&!suggest.getAuthor().isEmpty()&&!suggest.getEdition().isEmpty()) {
			booksService.insertSuggest(suggest);
			model.addAttribute("msg","提交成功");
			return "/users/suggestBuy";
		}  else {
			model.addAttribute("msg","请正确输入书的信息");
			return "/users/suggestBuy";
		}

	}
	
	/*
	 * 查看个人信息
	 */
	@RequestMapping("/person")
	public String person(Model model,HttpSession session) {
		UserModel user = (UserModel) session.getAttribute("user");
		UserInfo person=booksService.selectPersonMessage(user.getId());
		System.out.println(person);
		
		model.addAttribute("person", person);
		
		return "/users/personMessage";
		
	}
	
	/*
	 * 跳转到个人信息修改界面  
	 */
	@RequestMapping("/updatePersonForm")
	public String updatePersonForm(Model model, Integer uid) {
		
		UserInfo person=booksService.selectPersonMessage(uid);
		
		model.addAttribute("person",person);
		
		return "/users/updatePersonForm";
	}
	
	/*
	 * 修改个人信息
	 */
	@RequestMapping("/updatePerson")
	public String updatePerson(Model model,UserInfo personMessage) {
		
		System.out.println(personMessage);
		
		if(!personMessage.getPhone().isEmpty()) {
		booksService.updatePersonMessage(personMessage);
		}else {
			model.addAttribute("msg","至少填写电话，方便管理员联系");
			return "/users/updatePersonForm";
		}
		
		return "redirect:/user/person";
	}
	
}
